import 'package:dio/dio.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../../../net/dio_utils.dart';
import '../../../utils/toast_utils.dart';

class TrainTeacherDegreeOfStatisfactionItemController extends GetxController {
  TrainTeacherEntity trainTeacherEntity;

  TrainTeacherDegreeOfStatisfactionItemController(this.trainTeacherEntity);

  TrainUserCheckRecoderEntity _checkRecoderEntity;
  bool _isLoading = false;

  bool getIsLoading() {
    return _isLoading;
  }

  TrainUserCheckRecoderEntity getTrainUserCheckRecoderEntity() {
    return _checkRecoderEntity;
  }

  CancelToken _signCancelToken;

  Future<bool> commitTrainDegreeOfStatisfactionRequest(
      {bool click = false}) async {
    _isLoading = true;
    if (click) {
      update(["root"]);
    }

    if (_signCancelToken != null && !_signCancelToken.isCancelled) {
      _signCancelToken.cancel();
    }
    _signCancelToken = new CancelToken();
    List detreeDetails = [];
    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        detreeDetails.add(map);
      }
    }
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.trainTeacherDegreeOfStatisFacionAdd,
        cancelTag: _signCancelToken,
        jsonMap: {
          "trainId": trainTeacherEntity.trainId,
          "teacherId": trainTeacherEntity.teacherId,
          "teacherCourse": trainTeacherEntity.teacherContent,
          "courseId": trainTeacherEntity.id,
          "detreeDetails": detreeDetails,
          "attendanceNumber": getTotalScore(),
        });
    if (statisRresponseInfo.success) {
      Map data = statisRresponseInfo.data;
      _checkRecoderEntity = TrainUserCheckRecoderEntity.fromMap(data);
    } else {
      _isLoading = false;
      update(["root"]);
      ToastUtils.showToast("操作失败 " + statisRresponseInfo.message);
      return false;
    }
    _isLoading = false;
    ToastUtils.showToast("操作成功");
    Get.back(result: true);

    return true;
  }

  List<Map<String, dynamic>> list = [
    {
      "key": "一、对培训课程评价",
      "values": [
        {
          "key": "1、培训内容的实用性、系统性及理论与实际结合程度",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、培训内容丰富新颖，清晰明确，重点突出",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、培训内容的前后连贯性",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、培训内容编排合理，难易度适中",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、培训内容符合我的需要，具有针对性和指导性",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "6、培训内容与主题相符，课时分配合理",
          "index": 6,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    },
    {
      "key": "二、对培训教师的评价",
      "values": [
        {
          "key": "1、仪表得体，态度端正，口齿清楚，语言流利",
          "values": [1, 2, 3, 4, 5],
          "index": 1,
          "selectValue": "",
        },
        {
          "key": "2、备课准备充分，内容熟练，知识面广，条理清晰",
          "index": 2,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "3、培训方式方法多样化，讲课技巧娴熟有水平",
          "index": 3,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "4、专业经验丰富，课程生动有趣，通俗易懂",
          "index": 4,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
        {
          "key": "5、互动充分，气氛活泼，驾驭课堂能力强",
          "index": 5,
          "values": [1, 2, 3, 4, 5],
          "selectValue": "",
        },
      ],
    },
  ];

  int getTotalScore() {
    int total = 0;

    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        String selectValue = map["selectValue"];
        if (selectValue.isNotEmpty) {
          total += int.parse(selectValue);
        }
      }
    }
    return total;
  }

  int getNoTotalNumber() {
    int total = 0;

    for (int i = 0; i < list.length; i++) {
      Map<String, dynamic> map = list[i];
      List<Map<String, dynamic>> values = map["values"];
      for (int k = 0; k < values.length; k++) {
        Map<String, dynamic> map = values[k];
        String selectValue = map["selectValue"];
        if (selectValue.isEmpty) {
          total++;
        }
      }
    }
    return total;
  }
}
